package think.thread.c07.countdownlatch;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class TaskPortion implements Runnable{
	
	private static int count=0;
	private final int id=count++;
	private CountDownLatch latch;
	
	public TaskPortion(CountDownLatch latch){
		this.latch=latch;
	}
	
	private Random rand=new Random();
	
	public void run(){
		try {
			doWork();
			latch.countDown();
		} catch (Exception e) {
		}
	}
	public void doWork()throws Exception{
		TimeUnit.MILLISECONDS.sleep(rand.nextInt(500));
		System.out.println(this+ " completed");
	}
	
	public String toString() {
		return String.format("%1$-3d ", id);
	}
}
